package com.scuec.tourismwebsite.controller;
import com.scuec.tourismwebsite.entity.User;
import com.scuec.tourismwebsite.service.BoutiqueTourService;
import com.github.pagehelper.PageInfo;
import com.scuec.tourismwebsite.entity.BoutiqueTour;
import com.scuec.tourismwebsite.entity.UserOrder;
import com.scuec.tourismwebsite.service.UserOrderService;
import com.scuec.tourismwebsite.vo.OrderUserBoutique;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("tour/v1")
public class UserOrderController {
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private BoutiqueTourService boutiqueTourService;
    @DeleteMapping("order/{order_id}")
    public int deleteOrderById(@PathVariable Integer order_id){
        return userOrderService.deleteOrderById(order_id);
    }

    @GetMapping("orderpages")
    public PageInfo<OrderUserBoutique> findOrderPages(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageInfo<OrderUserBoutique> pageInfo=userOrderService.findOrderPages(pageNum,pageSize);
        return pageInfo;
    }

    @GetMapping("orderpage")
    public PageInfo<OrderUserBoutique> findOrderPage(String nickname,Integer pageNum, Integer pageSize) {
        PageInfo<OrderUserBoutique> pageInfo=userOrderService.findOrderPage(nickname,pageNum,pageSize);
        return pageInfo;
    }
    @GetMapping("anapages")
    public PageInfo<OrderUserBoutique> findAnaPages(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageInfo<OrderUserBoutique> pageInfo=userOrderService.findAnaPages(pageNum,pageSize);
        return pageInfo;
    }

    @GetMapping("anapage")
    public PageInfo<OrderUserBoutique> findAnaPage(String tour_name,Integer pageNum, Integer pageSize) {
        PageInfo<OrderUserBoutique> pageInfo=userOrderService.findAnaPage(tour_name,pageNum,pageSize);
        return pageInfo;
    }
    @GetMapping("orderAna")
    public List<OrderUserBoutique> findOrderToAll() {
        return userOrderService.findOrderToAll();
    }

    @GetMapping("orders/{user_id}")
    public List<OrderUserBoutique> findOrdersByUserId(@PathVariable Integer user_id) {
        return userOrderService.findOrdersByUserId(user_id);
    }

    @GetMapping("order/{order_id}")
    public ResponseEntity<UserOrder> findOrderById(@PathVariable Integer order_id) {
        try {
            UserOrder order = userOrderService.findOrderById(order_id);

            if (order == null) {
                return ResponseEntity.notFound().build(); // 404
            }

            return ResponseEntity.ok(order);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @PostMapping
    public int insertUserOrder(@RequestBody UserOrder userOrder) {
        return userOrderService.insertUserOrder(userOrder);
    }


    @PostMapping("createOrder")
    public ResponseEntity<String> createOrder(@RequestBody UserOrder userOrder, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("user_id");
        try {
            // 创建订单
            int orderResult = userOrderService.insertUserOrder(userOrder);
            if (orderResult <= 0) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("订单创建失败");
            }

            // 减少精品团的可预订数量
            int decreaseResult = boutiqueTourService.decreaseAvailability(userOrder.getBoutique_id());
            if (decreaseResult <= 0) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("精品团不可预订或不存在");
            }

            return ResponseEntity.ok("订单创建成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("订单创建失败：" + e.getMessage());
        }

    }

}